/*
 * @Author: your name
 * @Date: 2020-12-27 12:48:22
 * @LastEditTime: 2021-03-28 10:26:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \white-jotter-blog-vue\src\main.js
 */
import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";

import Meta from "vue-meta";
Vue.use(Meta);

import less from "less";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

// markdown
import mavonEditor from "mavon-editor";
import "mavon-editor/dist/css/index.css";

// 进度条
import NProgress from "nprogress";
import "nprogress/nprogress.css";

Vue.use(less);

Vue.use(ElementUI);

console.log("当前的base地址" + process.env.VUE_APP_BASE_URL)

// 引入axios
import axios from "axios";
// baseURL配置
if (process.env.VUE_APP_BASE_URL === "http://localhost:8111/api") {
  // 本地开发的话，需要配置跨域
  axios.defaults.baseURL = "/api";
} else {
  // 部署到服务器
  axios.defaults.baseURL = process.env.VUE_APP_BASE_URL
}

axios.interceptors.request.use((config) => {
  // 请求发起后就开启进度条
  NProgress.start();
  // 请求头添加 Token 验证的 Authorization 字段
  config.headers.Authorization = window.localStorage.getItem("token");
  return config;
});
axios.interceptors.response.use(
  (res) => {
    // 响应后就关闭进度条
    NProgress.done();
    return res;
  },
  (err) => {
    if (err.message === "Request failed with status code 401") {
      window.localStorage.clear();
      window.sessionStorage.clear();
      ElementUI.Notification({
        title: "提示",
        message: "用户身份过期，请重新登陆",
        position: "top-left",
        type: "warning",
      });
      router.replace("/login");
    }
  }
);
Vue.prototype.$http = axios;

// 自定义混入
import {
  mixin
} from "mixins/mixin.js";
Vue.mixin(mixin);

// use
Vue.use(mavonEditor);
Vue.prototype.$md = mavonEditor;

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");